/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.goldentrade.negocio.interfaces.impl.serviceLocator;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Andre
 */
public class ServiceLocator {

    private static EntityManagerFactory entityManagerFactory = javax.persistence.Persistence.createEntityManagerFactory ("GoldenTradePU");;
    private static EntityManager entityManager = null;

    private ServiceLocator() {
    }

    //sigleton
    public static EntityManager getEntityManager() {
        if (entityManager == null) {
            entityManager = entityManagerFactory.createEntityManager();
        }
        System.out.println("get entity manager: "+entityManager.isOpen());
        return entityManager;
    }

    public static void leaveEntityManager(EntityManager entityManager) {
        ServiceLocator.entityManager = entityManager;
        System.out.println("leave entity manager: "+entityManager.isOpen());
    }

    public static void destroyEntityManager() {
        ServiceLocator.entityManager = null;
    }
}
